Fix fork-time deadlock between ConfigLoader and threadSafeForkPrepare#1375
Open
robieta wants to merge 1 commit into
Open
Fix fork-time deadlock between ConfigLoader and threadSafeForkPrepare#1375robieta wants to merge 1 commit into
robieta wants to merge 1 commit into
Conversation
Summary: Fix a deadlock when `torch._thread_safe_fork` (used by `torch.compile`'s `SubprocPool`) triggers `threadSafeForkPrepare()` while kineto's ConfigLoader background thread is active. The deadlock occurs because: 1. `threadSafeForkPrepare()` acquires exclusive `guardLock`, then calls `SingletonVault::destroyInstances()` 2. The `kShutdownConfigLoaderOnSingletonVaultDestroy` callback fires and calls `ConfigLoader::stopThread()`, which tries to `join()` the ConfigLoader thread 3. The ConfigLoader thread is blocked trying to acquire a shared `ThreadSafeForkGuard` in `DynoConfigLoader::readBaseConfig()` — but cannot because `guardLock` is held exclusively by the forking thread This is a classic AB-BA deadlock: Thread A holds guardLock and waits for Thread B to join; Thread B waits for guardLock (shared) and cannot exit. The fix splits `stopThread()` into a non-blocking `signalStop()` (sets `stopFlag_` + notifies condvar) and the existing `stopThread()` (signal + join). The singleton vault callback now uses `signalStop()`. The ConfigLoader thread exits cooperatively on its next loop iteration after `guardLock` is released. The actual join is deferred to `~ConfigLoader()` at static-destruction time. Differential Revision: D102071920
|
@robieta has exported this pull request. If you are a Meta employee, you can view the originating Diff in D102071920. |
Author
CPU build failure looks unrelated to my changes. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Fix a deadlock when
torch._thread_safe_fork(used bytorch.compile'sSubprocPool) triggersthreadSafeForkPrepare()while kineto's ConfigLoaderbackground thread is active.
The deadlock occurs because:
threadSafeForkPrepare()acquires exclusiveguardLock, then callsSingletonVault::destroyInstances()kShutdownConfigLoaderOnSingletonVaultDestroycallback fires and callsConfigLoader::stopThread(), which tries tojoin()the ConfigLoader threadThreadSafeForkGuardinDynoConfigLoader::readBaseConfig()— but cannotbecause
guardLockis held exclusively by the forking threadThis is a classic AB-BA deadlock: Thread A holds guardLock and waits for
Thread B to join; Thread B waits for guardLock (shared) and cannot exit.
The fix splits
stopThread()into a non-blockingsignalStop()(setsstopFlag_+ notifies condvar) and the existingstopThread()(signal + join).The singleton vault callback now uses
signalStop(). The ConfigLoader threadexits cooperatively on its next loop iteration after
guardLockis released.The actual join is deferred to
~ConfigLoader()at static-destruction time.Differential Revision: D102071920